
<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
.bg-purple-dark {
  background: #000;
}
.el-row {
  margin-bottom: 20px;
}
.el-col {
  border-radius: 4px;
}
.el-link{
  margin-right: 5px;
}
</style>
<template>
  <div class="block">
    <addchapter ref="add"> </addchapter>
    <addperiod ref="addperiod"> </addperiod>

    <div style="margin-bottom: 30px">章节列表</div>
    <el-row>
      <el-button @click="hand">添加章节</el-button>
      <el-button @click="bratchchapter">批量添加章节</el-button>
      <el-button>刷新列表</el-button>
      <el-button>启用排序</el-button>
    </el-row>
    <el-collapse
      v-for="(val, index) in list"
      :key="val.id">
      <el-collapse-item :name='val.id' animation="300">
        <template slot="title">
          <el-row type="flex" justify="space-between" style="width: 100%; line-height: 15px" >
            <el-col :span="6" style="margin-left: 5px"><div><h4>第{{index+1}}章&nbsp; {{val.chapterName}}</h4></div></el-col>
            <el-col :span="6" style="margin-top:15px">
              <el-link type="primary" @click.stop="addperiod(val)">添加课时</el-link>
              <el-link type="primary" @click.stop="editChapter(val)">编辑章节</el-link>
              <el-link type="primary" @click.stop="removeChapter(val)">删除章节</el-link>              
            </el-col>
          </el-row>
        </template>
      </el-collapse-item>
    </el-collapse>

    <el-tree
      :data="data"
      show-checkbox
      node-key="id"
      default-expand-all
      :expand-on-click-node="false"
      draggable
      :allow-drop="allowDrop"
      :allow-drag="allowDrag">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>111</span>
        <span>
          <el-button type="text" size="mini" @click="() => appendPeriod(data)">
            添加课时
          </el-button>
          <el-button type="text" size="mini" @click="() => edit(date)">
            编辑章节
          </el-button>
          <el-button type="text" size="mini" @click="() => remove(node, data)">
            删除章节
          </el-button>
        </span>
      </span>
    </el-tree>
  </div>
</template>

<script>
import * as selectapi from "@/api/chapters.js";
import addchapter from "./addchapter";
import addperiod from "./addperiod";
let id = 1000;

export default {
  components: { addchapter, addperiod },
  data() {
    return {
      list: [],
      index: '', 
      data: [
        {
          id: '',
          chapterName: '',
          chapterDesc: '',
          sort: '',
          isFree: '',
          chapterOriginal: '',
          chapterDiscount: '',
        },
      ],
      title: '',
      courseId: ''
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      selectapi
        .getChapters(1)
        .then((response) => {
          this.list = response.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },

    hand() {
      this.$refs.add.addOpen(this.courseId);
    },
    addperiod() {
      this.$refs.addperiod.open(val, this.courseId);
    },
    editChapter(val){
      
      this.$refs.add.editOpen(val, this.courseId);
    },
    removeChapter(val){
      this.$refs.add.deleteOpen(val, this.courseId);
    },
    append(data) {
      const newChild = { id: id++, label: "testtest", children: [] };
      if (!data.children) {
        this.$set(data, "children", []);
      }
      data.children.push(newChild);
    },
    eidt(data) {},
    remove(node, data) {
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const index = children.findIndex((d) => d.id === data.id);
      children.splice(index, 1);
    },
    bratchchapter() {
      this.$router.push({
        path: '/example/chapters',
        query: {
          //参数
          courseId: this.courseId,
          sort: this.list.length+1
        }
      })
    },
    allowDrop() {},
    allowDrag() {},
  },
};
</script>
